//
//  PayOrderDetails.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/19.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "PayOrderDetails.h"
#import "myWkWebViewController.h"

@implementation PayOrderDetails



- (void)layoutSubviews{
    self.tableView.backgroundColor = [UIColor whiteColor];
}


- (UITableView *)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, self.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        [_tableView registerNib:[UINib nibWithNibName:@"OrderNoticeCell" bundle:nil] forCellReuseIdentifier:@"OrderNoticeCell"];
        //UITableView去掉自带系统的分割线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_tableView];
    }
    
    return _tableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(_data.count>0){
        return _data.count + 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *goods = _data;
    UITableViewCell *cell=nil;
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }else{
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
        }
    }
    if(goods.count > 0 & indexPath.row == goods.count){
        NSDictionary *dic = _info;
        UIView *allPrice = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 60)];
        [cell.contentView addSubview:allPrice];
        
        UIView *linview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, allPrice.width, GETHEIGHT(1))];
        linview.backgroundColor = [UIColor grayColor];
        [allPrice addSubview:linview];
        
        UILabel *xjlab = [[UILabel alloc] initWithFrame:CGRectMake(GETWITH(30), 10, allPrice.width-20, 20)];
        xjlab.text = @"小计";
        xjlab.font = font16;
        [allPrice addSubview:xjlab];
        
        UILabel *allpriceLab = [[UILabel alloc] initWithFrame:CGRectMake(allPrice.width/2-20, 10, allPrice.width/2-GETWITH(10), 20)];
        
        allpriceLab.text = [NSString stringWithFormat:@"￥%@",dic[@"price"]];
        allpriceLab.font = font16;
        allpriceLab.textAlignment = NSTextAlignmentRight;
        [allPrice addSubview:allpriceLab];
        if(dic[@"point_pay"]&&([dic[@"point_pay"] intValue]>0)){
            NSString *zlaStr = [NSString stringWithFormat:@"+%@紫喇叭",dic[@"point_pay"]];
            allpriceLab.width = allpriceLab.width - [ToolClass heighW:font14 titleStr:zlaStr].width;
            UILabel *zilaba = [[UILabel alloc] initWithFrame:CGRectMake(allpriceLab.right, allpriceLab.y,[ToolClass heighW:font14 titleStr:zlaStr].width , 20)];
            zilaba.text = zlaStr;
            zilaba.font = font14;
            zilaba.textColor =  UIColorFromRGB(0xfd5974);
            [cell.contentView addSubview:zilaba];
        }
        
        
        UILabel *ptlab = [[UILabel alloc] initWithFrame:CGRectMake(GETWITH(30), 35, 100,20)];
        ptlab.text = @"平台服务费";
        ptlab.textColor = [UIColor grayColor];
        ptlab.font = font12;
        [allPrice addSubview:ptlab];
        
        UILabel *money = [[UILabel alloc] initWithFrame:CGRectMake(allPrice.width/2-20, 35, allPrice.width/2-GETWITH(10), 20)];
        money.text = [NSString stringWithFormat:@"￥%@",dic[@"money"]];
        money.textAlignment = NSTextAlignmentRight;
        money.font = font12;
        money.textColor = [UIColor grayColor];
        [allPrice addSubview:money];
        
        
        
    }else{
        NSDictionary *dic = goods[indexPath.row];
        UILabel *shopname = [[UILabel alloc] init];
        shopname.font = font14;
        shopname.text = [NSString stringWithFormat:@"%@",dic[@"goods_name"]];
        [cell.contentView addSubview:shopname];
        shopname.sd_layout.leftSpaceToView(cell.contentView, GETWITH(30)).topSpaceToView(cell.contentView, 8).widthIs(0).autoHeightRatio(0);
        [shopname setSingleLineAutoResizeWithMaxWidth:GETWITH(300)];
        
        
        UILabel *shopNumber = [[UILabel alloc] init];
        shopNumber.font = font14;
        shopNumber.textAlignment = NSTextAlignmentCenter;
        shopNumber.text = [NSString stringWithFormat:@"x%@",dic[@"goods_number"]];
        [cell.contentView addSubview:shopNumber];
        shopNumber.sd_layout.rightSpaceToView(cell.contentView, GETWITH(300)).topEqualToView(shopname).widthIs(GETWITH(60)).autoHeightRatio(0);
        
        UILabel *shopPrice = [[UILabel alloc] init];
        shopPrice.font = font14;
        shopPrice.textAlignment = NSTextAlignmentRight;

        [cell.contentView addSubview:shopPrice];
        shopPrice.sd_layout.rightSpaceToView(cell.contentView, GETWITH(60)).topEqualToView(shopname).widthIs(GETWITH(300)).autoHeightRatio(0);
        
        if([dic[@"total_price"] isEqualToString:@"0.00"]){
            shopPrice.text = [NSString stringWithFormat:@"%@紫喇叭",dic[@"total_integral"]];
            shopPrice.textColor = UIColorFromRGB(0xfd5974);
        }else{
            if(dic[@"total_integral"] &&([dic[@"total_integral"] intValue]>0)){
                NSString *str = [NSString stringWithFormat:@"￥%@+%@",dic[@"total_price"],dic[@"total_integral"]];
                NSString *integral = dic[@"total_integral"];
                shopPrice.attributedText = [ToolClass AttributedString:str range:NSMakeRange(str.length-integral.length,integral.length) color:UIColorFromRGB(0xfd5974)];
            }else{
                shopPrice.text = [NSString stringWithFormat:@"￥%@",dic[@"total_price"]];
            }
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == _data.count) return;
    NSDictionary *dic = _data[indexPath.row];
    myWkWebViewController *vc = [[myWkWebViewController alloc] init];
    NSString *url = @"";
    if(dic[@"is_point"]){
      url = [NSString stringWithFormat:@"%@/ShopApi/Html/itemDetail?noticeId=%@&flag=2&isPoint=%@",APP_HOST_ADDRESS,dic[@"goods_id"],dic[@"is_point"]];
    }
    vc.myurl = url;
    vc.toTitle= @"商品详情";
    
    [[ToolClass viewController:self].navigationController pushViewController:vc animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     if(indexPath.row == _data.count){
         return 60;
     }
    NSDictionary *dic = _data[indexPath.row];
    CGSize titleSize = [dic[@"goods_name"] boundingRectWithSize:CGSizeMake(GETWITH(300), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font14 } context:nil].size;
    return titleSize.height+16;
}


@end
